package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.dao.SaleListDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-28 16:06 星期六
 * description:
 */
@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {

    @Autowired
    SaleListGoodsDao saleListGoodsDao;

    @Autowired
    UserDao userDao;

    @Autowired
    SaleListDao saleListDao;

    @Autowired
    GoodsService goodsService;

    /**
     * 获取商品销售的总数量
     * @param goodsId
     * @return
     */
    @Override
    public Integer getSumSaleGoods(Integer goodsId) {

        return saleListGoodsDao.getSumSaleGoods(goodsId) == null ? 0 : saleListGoodsDao.getSumSaleGoods(goodsId);
    }

    /**
     * 销售单保存
     * @param saleList
     * @param saleListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(SaleList saleList, String saleListGoodsStr) {

        //将saleListGoodsStr转换成实体类
        List<SaleListGoods> saleListGoodsList = JSONArray.parseArray(saleListGoodsStr, SaleListGoods.class);

        //获取当前用户id
        User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        saleList.setUserId(currentUser.getUserId());

        //保存saleList到数据库
        saleListDao.save(saleList);

        //获取saleListId
        Integer saleListId = saleList.getSaleListId();

        for (SaleListGoods saleListGoods : saleListGoodsList) {
            saleListGoods.setSaleListId(saleListId);

            //商品销售后  扣除库存
            Integer goodsNum = saleListGoods.getGoodsNum();
            String goodsCode = saleListGoods.getGoodsCode();
            boolean ifChange = goodsService.changeInventoryQuantity(goodsCode, -goodsNum);
            if (ifChange == false){
                return new ServiceVO(ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR,ErrorCode.CHANGE_INVENTORY_QUANTITY_ERROR_MSG);
            }

        }

        //批量保存saleListGoods
        saleListGoodsDao.bachSave(saleListGoodsList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 销售单商品信息
     * @param saleListId
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer saleListId) {

        Map<String, Object> map = new HashMap<>();
        List<SaleListGoods> saleListGoodsList = saleListGoodsDao.goodsList(saleListId);
        map.put("rows",saleListGoodsList);
        return map;
    }
}
